package han.cirno.corrupt.activity.ohno

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import com.google.android.material.snackbar.Snackbar
import han.cirno.corrupt.R
import han.cirno.corrupt.activity.BaseActivity
import han.cirno.corrupt.components.crash.CrashUtils.sendCrashReport
import han.cirno.corrupt.databinding.ActivityOhnoBinding

class OhnoActivity: BaseActivity() {

    private lateinit var ohnoBinding: ActivityOhnoBinding
    private lateinit var crashString:String
    private lateinit var crashFileParh:String
    private var warn=false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ohnoBinding=ActivityOhnoBinding.inflate(LayoutInflater.from(this))
        setContentView(ohnoBinding.root)
        crashString=intent.extras?.getString("report")?:"(Nullity)"
        crashFileParh=intent.extras?.getString("path")?:""

        ohnoBinding.activityOhnoShare.setOnClickListener {
            sendCrashReport(crashFileParh)
        }
        ohnoBinding.activityOhnoCopy.setOnClickListener {
            val clipboard=getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            clipboard.setPrimaryClip(ClipData.newPlainText("Corrupt",crashString))
            Snackbar.make(ohnoBinding.root,R.string.ohno_copied,Snackbar.LENGTH_SHORT).show()
        }
        ohnoBinding.activityOhnoClear.setOnLongClickListener {
            if (!warn){
                ohnoBinding.activityOhnoClear.setText(R.string.ohno_wipe_warn)
                warn=true
                return@setOnLongClickListener false
            }
            Runtime.getRuntime().exec("pm clear $packageName")
            ohnoBinding.activityOhnoClear.setText(R.string.ohno_wiped)
            warn=false
            return@setOnLongClickListener true
        }
        ohnoBinding.activityOhnoClear.setOnClickListener{
            ohnoBinding.activityOhnoClear.setText(R.string.ohno_wipe_warn)
            warn=true
        }
    }
}